﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;

/* cliente de ejemplo...
 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Net;

 namespace ConsoleApplication2
 {
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Proxy.Credentials = new NetworkCredential ("avarela", "gomagoma");
                string json = new string ('x', 40 * 120) + "j";
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                string ret = wc.UploadString("http://televoyeur.com/echo.aspx", "POST", "data=" + json);                
                Console.WriteLine(ret);
            }

            Console.ReadLine();
        }
    }
 }
*/

public partial class Echo : System.Web.UI.Page
{
    protected void Page_Load (object sender, EventArgs e)
    {
        Response.Write  ("Request.RequestType = " + Request.RequestType);
        Response.Write  ("\n");
        Response.Write  ("Request.RawUrl.Length = " + Request.RawUrl.Length.ToString());
        Response.Write  ("\n");
        Response.Write  ("Request.RawUrl = " + Request.RawUrl);

        if (Request.RequestType == "POST")
        {
            Response.Write("\n");
            Response.Write("MD5 = " + dameString16_MD5(System.Text.Encoding.ASCII.GetBytes(Request.Form["data"])));
            Response.Write("\n");
            Response.Write(Request.Form["data"].Length.ToString() + " bytes en data");
        }
    }

    public static byte[] dameMD5(byte[] buff)
    {
        return dameHash(buff, MD5.Create());
    }

    public static byte[] dameMD5(Stream stream)
    {
        return dameHash(stream, MD5.Create());
    }

    public static byte[] dameSHA1(byte[] buff)
    {
        return dameHash(buff, SHA1.Create());
    }

    public static byte[] dameHash(byte[] buff, HashAlgorithm h)
    {
        return h.ComputeHash(buff);
    }

    public static byte[] dameHash(Stream stream, HashAlgorithm h)
    {
        return h.ComputeHash(stream);
    }

    public static string dameString16_MD5(byte[] buff)
    {
        return reprHexa(dameMD5(buff));
    }

    public static string dameString16_MD5(Stream stream)
    {
        return reprHexa(dameMD5(stream));
    }

    public static string dameString16_SHA1(byte[] buff)
    {
        return reprHexa(dameSHA1(buff));
    }

    public static string reprHexa(byte[] buff)
    {
        string ret = "";

        foreach (byte b in buff)
            ret += b.ToString("X2");

        return ret;
    }
}
